package org.numb.concurrency.chapter01;

public class Reorder {

    public static int a = 0;
    public static int b = 0;
    public static int x = 0;
    public static int y = 0;

    private static int times = 0;

    public static void init() {
        a = 0;
        b = 0;
        x = 0;
        y = 0;
    }

    public static boolean reorder() throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            a = 1;
            x = b;
        });

        Thread thread2 = new Thread(() -> {
            b = 1;
            y = a;
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        times++;
        System.out.println("第 "+ times + " 结果：" + "(x, y) = " + x + ", " + y);
        return x == 0 && y == 0;
    }

}
